<template>
  <div>
    <div class="mess-body">
      <div class="mess-item">
        <div class="mess-head">
          <div class="left-btn-group">
            <el-button type="danger">添加</el-button>
            <el-button type="danger">删除</el-button>
          </div>
          <div class="right-group">
            <el-input
              placeholder="请输入用户名"
              style="max-width: 200px"
              v-model="searchtext"
            ></el-input>
            <el-button type="danger">查找</el-button>
          </div>
        </div>
        <div class="mess-table">
          <el-table :data="searchmessagelist" style="width: 100%">
            <el-table-column prop="username" label="姓名" width="180">
            </el-table-column>
            <el-table-column prop="useremail" label="邮箱" width="180">
            </el-table-column>
            <el-table-column prop="usermessage" label="内容"> </el-table-column>
          </el-table>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import { getusermessage } from "network/request.js";

export default {
  data() {
    return {
      messagelist: [],
      searchtext: ''
    };
  },
  computed: {
    //搜索框输入显示对应用户
    searchmessagelist: function () {
      if (this.searchtext == "") {
        return this.messagelist;
      } else {
        var arrByZM = []; //声明一个空数组来存放数据
        for (var i = 0; i < this.messagelist.length; i++) {
          //for循环数据中的每一项（根据name值）
          if (this.messagelist[i].username.search(this.searchtext) != -1) {
            //判断输入框中的值是否可以匹配到数据，如果匹配成功
            arrByZM.push(this.messagelist[i]);
            //向空数组中添加数据
          }
        }
        //一定要记得返回筛选后的数据
        return arrByZM;
      }
      //逻辑-->根据input的value值筛选goodsList中的数据
    },
  },
  created() {
    //获取用户信息列表
    getusermessage().then(({ data: res }) => {
      this.messagelist = res;
      // console.log(this.messagelist);
    });
  },
};
</script>

<style lang="scss" scoped>
// .message-head {
//   width: 100%;
//   height: 100%;
// }
* {
  margin: 0;
  padding: 0;
  vertical-align: top;
}
.mess-body {
  width: 100%;
}

.mess-item {
  max-width: 1100px;
  height: calc(100vh - 140px);
  margin: 0 auto;
  margin-top: 20px;
  background-color: white;
}
.mess-head {
  width: 100%;
  height: 80px;
  /* background-color: green; */
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
}
.left-btn-group {
  /* background-color: blue; */
  height: 40px;
  width: 20%;
  display: flex;
  justify-content: space-between;
  margin: auto 0 auto 2%;
}
.right-group {
  height: 40px;
  width: 30%;
  /* background-color: yellow; */
  margin: auto 2% auto 0;
}
.right-group .el-button {
  height: 40px;
  width: 30%;
}
.right-group .el-input {
  height: 40px;
  margin-right: 5%;
}
.left-btn-group .el-button {
  height: 40px;
  width: 45%;
}
.mess-table {
  width: 90%;
  margin: 0 auto;
  border: 1px solid #ddd;
}
</style>